<?php

namespace app\modules\setting\controllers\seller;

use app\common\controllers\AdminOptionsController;
use app\common\enum\EtcSellerTiktokProduct_shStatusEnum;
use app\common\helper\CommonHelper;
use Yii;
use app\modules\setting\models\seller\EtcSellerTiktokProduct;
use app\modules\setting\configs\seller\KtbcpConfig;
use app\modules\setting\models\seller\EtcSellerTiktokProductSearch;
use sookie\react\AdminController;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\base\Exception;
use sookie\excel\SaveExcel;
use sookie\db\DBHandler;
use sookie\react\handlers\ImportHandler;


/**
 * KtbcpController implements the CRUD actions for EtcSellerTiktokProduct model.
 */
class KtbcpController extends AdminOptionsController
{
    /**
    * @return array
    * 列表数据
    */
    public function actionIndex()
    {
        try {
            $searchModel = new EtcSellerTiktokProductSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            $data = $dataProvider->getModels();
            if($data){
                $data = array_map(function($item){
                    $re = $item;
                    if(is_object($item)){
                        $re = $item->toArray();
                    }
                    $re['name'] = CommonHelper::getProductName($item);
                    $re['seller_commission_percent'] = CommonHelper::float($item['seller_commission_percent'])."%";
                    $re['creator_commission_percent'] = CommonHelper::float($item['creator_commission_percent'])."%";
                    return $re;
                },$data);
            }
            return ArrayHelper::merge(["data" =>$data, "pagination" => $dataProvider->getPagination()],KtbcpConfig::configIndex());
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }



    /**
    * @param $id
    * @return array
    * 修改接口
    */
    public function actionUpdate($id)
    {
        try {

            $model = $this->findModel($id);

            if (Yii::$app->request->isPost){
                if($model->sh_status == EtcSellerTiktokProduct_shStatusEnum::PASS){
                    throw new Exception("已审核通过，无需提交");
                }
                if($model->is_apply == 1){
                    throw new Exception("已提交审核，请等待审核结果");
                }
                $_POST['is_edit'] = 1;
                $_POST['is_apply'] = 1;
                $_POST['sh_status'] = 0;
                if ($model->load(Yii::$app->request->post(),"") && DBHandler::adjustFieldLength(EtcSellerTiktokProduct::tableName(),Yii::$app->request->post()) && $model->save()) {
                    return ["msg" => "修改成功"];
                }else{
                    return ["ok" => false,"msg" => $model->getFirstErrorStr(),"code" => 401];
                }
            }
            return KtbcpConfig::configUpdate($model);
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }



    protected function findModel($id)
    {
        if (($model = EtcSellerTiktokProduct::findOne($id)) !== null) {
            return $model;
        }

        throw new Exception('不存在的记录');
    }




}

